package controllers;

import java.sql.Date;
import java.util.LinkedList;

import org.apache.commons.mail.EmailException;

import antlr.collections.List;

import models.*;
import play.data.validation.Required;
import play.mvc.Controller;
import play.mvc.With;

@With(Secure.class)
// this class is used for the login, Doaa El-Hawary
public class Notifications extends Controller {

	/**
	 * notify many users
	 * 
	 * @author Tarek Sheasha
	 * @version 2011.04.25
	 * @category C1D9
	 * @param userId
	 *            : the sender of the notification, userIds: the users that will
	 *            receive the notification, notContent: the content of the
	 *            notification, notDate : the date of the notification
	 * 
	 * @return nothing
	 */
	public static void notifyMany(long userId, long[] userIds, String subject,
			String notContent) throws EmailException {
		User notSender = User.findById(userId);
		LinkedList<User> users = new LinkedList();
		User temp;
		for (int i = 0; i < userIds.length; i++) {
			temp = User.findById(userIds[i]);
			users.add(temp);
		}
		for (int i = 0; i < users.size(); i++) {
			notSender.sendNotification(users.get(i), subject, notContent);
			users.get(i).save();
		}
	}

	/**
	 * notify the creator of a tag by any comment added
	 * 
	 * @author Mohamed Shalaby
	 * @version 2011.04.26
	 * @category C2 S22
	 * @param sender
	 *            : the sender of the notification, tag: the tag that the user
	 *            comment it , Comment: the comment added or edited, subject:
	 *            "notify", notDate : the date of the notification
	 * @return nothing
	 * @throws EmailException
	 */

	public static void notifyCreatorOfTagByComment(long senId, long tagId,
			long comId, String subject, String date) throws EmailException {

		User sender = User.findById(senId);
		Tag tag = Tag.findById(tagId);
		Comment comment = Comment.findById(comId);

		Notification notify = new Notification(sender, tag.getOwner(), subject, (comment
				.getContent() + " Comment added or edited"), date);
		sender.sendNotification(tag.getOwner(), subject,
				(comment.getContent() + " Comment added or edited"));
		notify.save();
		sender.save();
	}

	/**
	 * notify the creator of a tag by any Rating added
	 * 
	 * @author Mohamed Shalaby
	 * @version 2011.04.26
	 * @category C2 S22
	 * @param sender
	 *            : the sender of the notification, tag: the tag that the user
	 *            comment it , r : the rate changed, subject: "notify", notDate
	 *            : the date of the notification
	 * @return nothing
	 * @throws EmailException
	 */

	public static void notifyCreatorOfTagByRating(long senId, long tagId,
			long ratId, String subject, String date) throws EmailException {

		User sender = User.findById(senId);
		Tag tag = Tag.findById(tagId);
		Rating rat = Comment.findById(ratId);

		Notification notify = new Notification(sender, tag.getOwner(), subject, (rat
				.getRating() + " Rating changed"), date);
		sender.sendNotification(tag.getOwner(), subject,
				(rat.getRating() + "Rating changed"));
		notify.save();
		sender.save();
	}

	/**
	 * notify the creator of an entity by any comment added
	 * 
	 * @author Mohamed Shalaby
	 * @version 2011.04.26
	 * @category C2 S22
	 * @param sender
	 *            : the sender of the notification, ent: the entity that the
	 *            user comment it , Comment: the comment added or edited,
	 *            subject: "notify", notDate : the date of the notification
	 * @return nothing
	 * @throws EmailException
	 */
	public static void notifyCreatorOfEntityByComment(long senId, long entId,
			long comId, String subject, String date) throws EmailException {

		User sender = User.findById(senId);
		Eentity entity = Eentity.findById(entId);
		Comment comment = Comment.findById(comId);

		Notification notify = new Notification(sender, entity.getOwner(),
				subject, (comment.getContent() + " Comment added or edited"),
				date);
		sender.sendNotification(entity.getOwner(), subject, (comment
				.getContent() + " Comment added or edited"));
		notify.save();
		sender.save();
	}

	/**
	 * notify the creator of an entity by any Rating added
	 * 
	 * @author Mohamed Shalaby
	 * @version 2011.04.26
	 * @category C2 S22
	 * @param sender
	 *            : the sender of the notification, ent: the entity that the
	 *            user comment it , r : the rate changed, subject: "notify",
	 *            notDate : the date of the notification
	 * @return nothing
	 * @throws EmailException
	 */
	public static void notifyCreatorOfEntityByRating(long senId, long entId,
			long ratId, String subject, String date) throws EmailException {

		User sender = User.findById(senId);
		Eentity entity = Eentity.findById(entId);
		Rating rat = Comment.findById(ratId);

		Notification notify = new Notification(sender, entity.getOwner(), subject, (rat
				.getRating() + " Rating changed"), date);
		sender.sendNotification(entity.getOwner(), subject,
				(rat.getRating() + " Rating changed"));
		notify.save();
		sender.save();
	}
}
